﻿using System;
using System.Collections.Generic;
using SystemX.Utility;

namespace SystemX.Infrastructure
{
	/// <summary>
	/// business object messager
	/// </summary>
	public class BOMessager
	{
		#region .ctor
		/// <summary>
		/// default .ctor
		/// </summary>
		public BOMessager() {
			Body = string.Empty;
			Context = new Dictionary<string, object>();
		}
		#endregion

		#region member variables
		private Dictionary<string, object> context=new Dictionary<string,object>();
		/// <summary>
		/// Const msg for NO record
		/// </summary>
		public static string NORECORD = "没有任何相关记录";
		public static string NORECORD_X = "没有任何相关记录-{0}";
		/// <summary>
		/// Const Msg for exception
		/// </summary>
		public static string EXCEPTION = "应用程序发生例外";
		public static string EXCEPTION_X = "应用程序发生例外-{0}";
        public static string NOTLOGINED = "未登录或者登录超时,请重新登陆";
		#endregion

		#region properties
		/// <summary>
		/// Is Error Message?
		/// </summary>
		public bool IsError {
			get;set;
		}
		/// <summary>
		/// Msg body
		/// </summary>
		public string Body {
			get;set;
		}
		/// <summary>
		/// other conext on the message
		/// </summary>
		public Dictionary<string, object> Context
		{
			get;set;
		}
		#endregion
		public BOMessager Error<T>(Exception ex) {
			return Error<T>(ex, false);
		}
		public BOMessager Error<T>(Exception ex, bool log) {
			IsError = true;
			Body = string.Format("{0}:{1}", EXCEPTION, ex.Message);
			if (log) {
				Logger<T>.Current.Error(ex.Message, ex);
			}
			return this;
		}
		public BOMessager Error<T>(string tipsFormatStr, params object[] args) {
			IsError = true;
			if (args.Length > 0)
			{
				Body = String.Format("{0}:{1}", typeof(T).FullName, string.Format(tipsFormatStr, args));
			}
			else {
				Body = String.Format("{0}:{1}", typeof(T).FullName, tipsFormatStr);
			}
			return this;
		}
	}
}
